package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private BlackListApi blackListApi;

    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        settingsVo.setId(userId);
        settingsVo.setPhone(UserHolder.getMobile());
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你会努力学习吗？" : question.getTxt();
        settingsVo.setStrangerQuestion(txt);
        Settings settings = settingsApi.findByUserId(userId);
        settingsVo.setStrangerQuestion(txt);
        if (settings != null) {
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        return settingsVo;
    }

    public HashMap<Object, Object> saveQuestion(String content) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findByUserId(userId);
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            question.setTxt(content);
            questionApi.update(question);
        }
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("userId", userId);
        map.put("msg", "设置问题成功");
        return map;
    }


    public Map<Object, Object> saveSettings(Map map) {

        Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        Boolean likeNotification = (Boolean) map.get("likeNotification");
        Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");

        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findByUserId(userId);
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        } else {
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        }
        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "设置通知信息成功");
        resultMap.put("userId", userId);
        return resultMap;
    }

    public PageResult blacklist(int page, int size) {
        Long userId = UserHolder.getUserId();
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, size);
        PageResult pr = new PageResult(page, size, iPage.getTotal(), iPage.getRecords());
        return pr;
    }

    public void deleteBlackList(Long blackUid) {
        Long userId = UserHolder.getUserId();
        blackListApi.deleteBlackList(userId, blackUid);
    }
}
